App({
    onLaunch() {
        const windowInfo = wx.getWindowInfo();
        const menuButtonInfo = wx.getMenuButtonBoundingClientRect();
        //状态栏高度
        let statusBarHeight = windowInfo.statusBarHeight;
        // 导航栏高度(标题栏高度) = 胶囊高度 + (顶部距离 - 状态栏高度) * 2
        let navBarHeight = menuButtonInfo.height + (menuButtonInfo.top - statusBarHeight) * 2;
        // 安全区域高度
        // let safeHeight = windowInfo.safeArea.height;
        // 安全区域 = 安全区域高度 - 导航栏高度
        // let bottomSafeHeight = windowInfo.screenHeight - windowInfo.safeArea.height - statusBarHeight;
        this.globalData.statusBarHeight = statusBarHeight
        this.globalData.navBarHeight = navBarHeight

        if (!wx.getStorageSync('userInfo')) {
            this.chooseTheme(0)
        } else {
            this.chooseTheme(wx.getStorageSync('userInfo').theme)
            this.globalData.loginToAdd = 1
        }

    },
    onShow() {
        // 检查更新
        this.update()
    },

    globalData: {
        // url: "https://52014117cyxy.vicp.fun",
        url: "https://ka.cancan.life",
        // url: "http://127.0.0.1:38080",
        userInfo: null,
        cache_year_index: 0,
        cache_month_index: 0,
        cache_year_stc: 0,
        cache_month_stc: 0,
        loginToAdd: 0,

        statusBarHeight: 0,
        navBarHeight: 0
    },

    chooseTheme(a) {
        if (a == 1) {
            // 粉黄色
            wx.setTabBarStyle({
                selectedColor: '#f3a7c5'
            })
            wx.setTabBarItem({
                index: 0,
                selectedIconPath: '/images/index3.png'
            })
            wx.setTabBarItem({
                index: 1,
                selectedIconPath: '/images/stac3.png'
            })
            wx.setTabBarItem({
                index: 2,
                selectedIconPath: '/images/compute3.png'
            })
            wx.setTabBarItem({
                index: 3,
                selectedIconPath: '/images/oil3.png'
            })
            wx.setTabBarItem({
                index: 4,
                selectedIconPath: '/images/my3.png'
            })
            this.globalData.configColor = `
        --backGroudColorMost: #f3a7c5; 
        --backGroudColorMostDown: #eebfd3;
        --backGroudColorLess: #deb13c;
        --backGroudColorLessDown: #e8b71a;`
            this.globalData.configColorForOnlyStyle = `background-color: #f3a7c5;`
            this.globalData.ColorMost = '#f3a7c5'
            this.globalData.ColorLess = '#deb13c'

        } else if (a == 2) {
            // 粉紫色
            wx.setTabBarStyle({
                selectedColor: '#f3a7c5'
            })
            wx.setTabBarItem({
                index: 0,
                selectedIconPath: '/images/index3.png'
            })
            wx.setTabBarItem({
                index: 1,
                selectedIconPath: '/images/stac3.png'
            })
            wx.setTabBarItem({
                index: 2,
                selectedIconPath: '/images/compute3.png'
            })
            wx.setTabBarItem({
                index: 3,
                selectedIconPath: '/images/oil3.png'
            })
            wx.setTabBarItem({
                index: 4,
                selectedIconPath: '/images/my3.png'
            })
            this.globalData.configColor = `
    --backGroudColorMost: #f3a7c5; 
    --backGroudColorMostDown: #eebfd3;
    --backGroudColorLess: #bd9fd9;
    --backGroudColorLessDown: #eeb8ee;`
            this.globalData.configColorForOnlyStyle = `background-color: #f3a7c5;`
            this.globalData.ColorMost = '#f3a7c5'
            this.globalData.ColorLess = '#bd9fd9'
        } else {
            wx.setTabBarStyle({
                selectedColor: '#3eb477'
            })
            wx.setTabBarItem({
                index: 0,
                selectedIconPath: '/images/index2.png'
            })
            wx.setTabBarItem({
                index: 1,
                selectedIconPath: '/images/stac2.png'
            })
            wx.setTabBarItem({
                index: 2,
                selectedIconPath: '/images/compute2.png'
            })
            wx.setTabBarItem({
                index: 3,
                selectedIconPath: '/images/oil2.png'
            })
            wx.setTabBarItem({
                index: 4,
                selectedIconPath: '/images/my2.png'
            })
            // 绿黄色
            this.globalData.configColor = `
    --backGroudColorMost: #3eb477; 
    --backGroudColorMostDown: #6fb885;
    --backGroudColorLess: #deb13c;
    --backGroudColorLessDown: #e8b71a;`
            this.globalData.configColorForOnlyStyle = `background-color: #3eb477`
            this.globalData.ColorMost = '#3eb477'
            this.globalData.ColorLess = '#deb13c'
        }

        wx.setStorageSync('ColorMost', this.globalData.ColorMost);
        wx.setStorageSync('ColorLess', this.globalData.ColorLess);
        wx.setStorageSync('configColor', this.globalData.configColor);
        wx.setStorageSync('configColorForOnlyStyle', this.globalData.configColorForOnlyStyle);

        // 延迟 100ms 后刷新
        // setTimeout(() => {
        //     wx.hideTabBar({
        //         success: () => {
        //             wx.showTabBar();
        //         }
        //     });
        // }, 100);
        wx.hideTabBar({
            success: () => {
                wx.showTabBar();
            }
        });
    },

    update() {
        const updateManager = wx.getUpdateManager()
        updateManager.onCheckForUpdate(function (res) {
            // 请求完新版本信息的回调
            if (res.hasUpdate) {
                // 新版本下载成功
                updateManager.onUpdateReady(function () {
                    wx.showModal({
                        title: '更新提示',
                        content: '新版本已经准备好，请您重启应用，以确保正常使用。',
                        success: function (res) {
                            if (res.confirm) {
                                // 新的版本已经下载好，调用 applyUpdate 应用新版本并重启
                                updateManager.applyUpdate()
                            }
                        }
                    })
                })
                // 新版本下载失败
                updateManager.onUpdateFailed(function () {
                    wx.showModal({
                        title: '更新提示',
                        content: '新版本已经上线啦~，请您删除当前小程序，重新搜索打开哟~'
                    })
                })

            }
        })
    }
})